<template>
  <div class="article-table">
    <div class="header">
      <div class="total">文章总数:{{ total }}</div>
      <div>
        <el-select placeholder="请选择" v-model="tag_id" @change="setTagId" v-if="tagList.length > 0">
          <el-option
            v-for="item in tagList"
            :key="item.tag_id"
            :label="item.tag_name"
            :content="item.tag_id"
            :value="item.tag_id"
          ></el-option>
        </el-select>
        <el-input placeholder="输入文章关键字" v-model="searchKey" class="search-input">
          <i
            v-if="isSearch"
            @click="clearSearch"
            slot="suffix"
            class="el-input__icon el-icon-close btn"
          ></i>
          <i @click="search" slot="suffix" class="el-input__icon el-icon-search btn"></i>
        </el-input>
        <el-button @click="openCreate" type="primary">添加文章</el-button>
      </div>
    </div>
    <el-table :data="tableData" height="calc(100% - 110px)" style="width: 100%">
      <el-table-column prop="title" label="文章标题"></el-table-column>
      <el-table-column prop="type" label="类型"></el-table-column>
      <el-table-column prop="keyword" label="关键字"></el-table-column>
      <el-table-column prop="real_name" label="作者"></el-table-column>
      <el-table-column label="添加时间">
        <template
          slot-scope="scope"
        >{{dayjs(Number(scope.row.create_time)).format('YYYY-MM-DD HH:mm:ss')}}</template>
      </el-table-column>
      <el-table-column label="更新时间">
        <template
          slot-scope="scope"
        >{{ scope.row.update_time ? dayjs(Number(scope.row.update_time)).format('YYYY-MM-DD HH:mm:ss') : '-'}}</template>
      </el-table-column>
      <el-table-column prop="comment_total" label="留言"></el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <div>
            {{
            scope.row.status === 1
            ? "发布"
            : scope.row.status === 0
            ? "保存"
            : "屏蔽"
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <div class="operation">
            <el-button @click="openDelete(scope.row)" type="danger" size="small">删除</el-button>
            <el-button @click="openEdit(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="openDetail(scope.row)" type="text" size="small">查看</el-button>
            <el-button
              v-if="scope.row.status !== 2"
              @click="handleShield(scope.row)"
              type="text"
              size="small"
            >屏蔽</el-button>
            <el-button v-else @click="handlePublish(scope.row)" type="text" size="small">发布</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :page-size="pageSize"
      :current-page="page"
      @current-change="pageChange"
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import dayjs from "dayjs";
import { createNamespacedHelpers } from "vuex";
const { mapActions,mapState } = createNamespacedHelpers("admin");
export default {
  data() {
    return {
      isEdit: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      article_name: "",

      pageSize: 10,
      page: 1,
      article_id: 1,
      tableData: [],
      total: 0,

      searchKey: "",
      isSearch: false,

      tagList: [],
      tag_id: "",
    };
  },
  computed: {
    ...mapState(["userInfo"])
  },
  mounted() {
    this.fetchArticleList();
    this.fetchTagList();
  },
  methods: {
    dayjs,
    ...mapActions([
      "getArticlelist",
      "deleteArticle",
      "articlePublish",
      "articleShield",
      "getTaglist",
      "searchArticle"
    ]),
    fetchTagList() {
      this.getTaglist().then(res => {
        this.tagList = res.list || [];
      });
    },
    setTagId(id) {
      this.tag_id = id;
      this.page = 1;
      this.fetchArticleList();
    },
    openDelete(row) {
      this.$confirm("确认删除？")
        .then(() => {
          this.article_id = row.article_id;
          this.deleteClick();
        })
        .catch(() => {});
    },
    openCreate() {
      this.$router.push({
        name: "admin-article-editor"
      });
    },
    openDetail(data) {
      this.$router.push({
        name: "admin-article-detail",
        query: {
          id: data.article_id
        }
      });
    },
    openEdit(data) {
      this.$router.push({
        name: "admin-article-editor",
        query: {
          isUse: "edit",
          id: data.article_id
        }
      });
    },
    handlePublish(row) {
      const { article_id } = row;
      this.articlePublish(article_id).then(() => {
        this.fetchArticleList();
      });
    },
    handleShield(row) {
      const { article_id } = row;
      this.articleShield(article_id).then(() => {
        this.fetchArticleList();
      });
    },
    deleteClick() {
      this.deleteArticle(this.article_id).then(() => {
        this.fetchArticleList();
      });
    },
    fetchArticleList() {
      const params = {
        page: this.page,
        pageSize: this.pageSize,
        user_id: this.userInfo.user_id,
      };
      if (this.tag_id) {
        params.tag_id = this.tag_id;
      }
      this.getArticlelist(params).then(res => {
        this.tableData = res.list;
        this.total = res.total;
      });
    },
    pageChange(res) {
      this.page = res;
      this.fetchArticleList();
    },
    search() {
      if ( this.searchKey === "" ) return;
      this.searchArticle(this.searchKey).then(res => {
        this.tableData = res.list;
      }) 
    },
    clearSearch() {}
  }
};
</script>

<style lang="scss" scoped>
.article-table {
  height: 100%;
  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64px;
    overflow-x: auto;
    .search-input {
      width: 180px;
      margin: 0 24px;
    }
  }
  .el-table {
    //   height: calc(100% - 110px);
    //   overflow: hidden;
    // /deep/  .el-table__body-wrapper {
    //     height: calc(100% - 48px);
    //     overflow: auto;
    //   }
  }

  .operation {
    display: flex;
  }
}
</style>
